﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


//@author: Lénia Andrade nº 5607


namespace LP_C_Sharp_2011_2012
{   
    public partial class rebides_application : Form
    {
        CreateDatabase  bd = new CreateDatabase();
        Statistics st = new Statistics();
        public rebides_application()
        {
            InitializeComponent();
            verifyDB();
        }

        /* This method is called  when this button is pressed
         calculates the first statistic*/

        private void calculate_establishments_Click(object sender, EventArgs e)
        {
            establishments_textBox.Clear();

            List<string> establishments_year = new List<string>();

            int y = int.Parse(year_comboBox_1.SelectedItem.ToString());
           
            establishments_year = st.establishments_per_year(y);
            string result = string.Format("In {0} there were {1} establishments"+ "\n", y,establishments_year.Count());
            establishments_textBox.AppendText(result);

        }

        /* This method is called  when this button is pressed
       calculates the first statistic for all years*/

        private void calculate_establishments_all_years_Click(object sender, EventArgs e)
        {

            establishments_textBox.Clear();

            List<string> establishments_year = new List<string>();
            
            for (int y = 2000; y <= 2010; y++)
            {
                establishments_year = st.establishments_per_year(y);
                string result = string.Format("In {0} there were {1} establishments" + "\n", y, establishments_year.Count());
                establishments_textBox.AppendText(result);
            }
        }

        /* This method is called  when this button is pressed
       calculates the second statistic*/

        private void calculate_teachers_2_Click(object sender, EventArgs e)
        {
            teachers_textBox_2.Clear();

            Dictionary<string, int> teachers_per_category = new Dictionary<string, int>();

            int y = int.Parse(year_comboBox_2.SelectedItem.ToString());

            teachers_per_category = st.teachers_per_category_per_year(y);

            string result = string.Format("In {0} " + "\n", y);
            teachers_textBox_2.AppendText(result);

            foreach (KeyValuePair<string, int> pair in teachers_per_category)
            {
                string result_2 = string.Format("As {1} there where {2} teachers"+ "\n",y,pair.Key,pair.Value);
                teachers_textBox_2.AppendText(result_2);
            }
            
      

        }

        /* This method is called  when this button is pressed
      calculates the second statistic for all years*/

        private void calculate_teachers_all_years_2_Click(object sender, EventArgs e)
        {

            teachers_textBox_2.Clear();

            Dictionary<string, int> teachers_per_category = new Dictionary<string, int>();

            for (int y = 2000; y <= 2010; y++)
            {
                teachers_per_category = st.teachers_per_category_per_year(y);

                string result = string.Format("In {0} " + "\n", y);
                teachers_textBox_2.AppendText(result);

                foreach (KeyValuePair<string, int> pair in teachers_per_category)
                {
                    string result_2 = string.Format("As {1} there where {2} teachers" + "\n", y, pair.Key, pair.Value);
                    teachers_textBox_2.AppendText(result_2);
                }

                teachers_textBox_2.AppendText("\n");

            }
            
        }

        /* This method is called when a user chooses one year in combobox (from the third statistic) 
         and so the establishment combobox is filled with existing establishments in the year chosen by the user */

        private void year_comboBox_3_SelectedIndexChanged(object sender, EventArgs e)
        {          
            List<string> establishments_year = new List<string>();

            int y = int.Parse(year_comboBox_3.SelectedItem.ToString());

            establishments_year = st.establishments_per_year(y);

            establishment_comboBox_3.Items.Clear();
         
            foreach(string h in establishments_year){

                establishment_comboBox_3.Items.Add(h);

            }
            establishment_comboBox_3.Enabled = true;
            year_comboBox_3.Enabled = false;

        }


        /* This method is called when a user chooses one establishment in combobox (from the third statistic)*/

        private void establishment_comboBox_3_SelectedIndexChanged(object sender, EventArgs e)
        {
            year_comboBox_3.Enabled = false;
            establishment_comboBox_3.Enabled = false;
            calculate_teachers_3.Enabled = true;    
        }


        /* This method is called  when this button is pressed
       calculates the third statistic*/

        private void calculate_teachers_3_Click(object sender, EventArgs e)
        {
            teachers_textBox_3.Clear();

            Dictionary<string, int> teachers_per_category_per_establishment = new Dictionary<string, int>();

            int y = int.Parse(year_comboBox_3.SelectedItem.ToString());
            string estab = establishment_comboBox_3.SelectedItem.ToString();

            teachers_per_category_per_establishment = st.teachers_per_category_per_establishment_per_year(y,estab);

            string result = string.Format("In {0} " + "\n" + "{1} \n", y,estab);
            teachers_textBox_3.AppendText(result);

            foreach (KeyValuePair<string, int> pair in teachers_per_category_per_establishment)
            {
                string result_2 = string.Format("As {1} there where {2} teachers" + "\n", y, pair.Key, pair.Value);
                teachers_textBox_3.AppendText(result_2);
            }

            year_comboBox_3.Enabled = true;
            year_comboBox_3.Text = "choose a year...";
            establishment_comboBox_3.Text = "choose a establishment...";
            calculate_teachers_3.Enabled = false;

        }


        /* This method is called  when this button is pressed
      calculates the third statistic for all the years*/

        private void calculate_teachers_all_years_3_Click(object sender, EventArgs e)
        {

            {
                if (MessageBox.Show("This operation may take a few minutes \n Do you want to continue?", "Rebides Application",
                      MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                      == DialogResult.Yes)
                {
                    teachers_textBox_3.Clear();

                    Dictionary<string, int> teachers_per_category_per_establishment = new Dictionary<string, int>();


                    establishments_textBox.Clear();

                    List<string> establishments_year = new List<string>();

                    for (int y = 2000; y <= 2010; y++)
                    {
                        establishments_year = st.establishments_per_year(y);

                        foreach (string estab in establishments_year)
                        {
                            teachers_per_category_per_establishment = st.teachers_per_category_per_establishment_per_year(y, estab);
                            string result = string.Format("In {0} " + "\n" + "{1} \n", y, estab);
                            teachers_textBox_3.AppendText(result);

                            foreach (KeyValuePair<string, int> pair in teachers_per_category_per_establishment)
                            {
                                string result_2 = string.Format("As {1} there where {2} teachers" + "\n", y, pair.Key, pair.Value);
                                teachers_textBox_3.AppendText(result_2);
                            }
                        }

                    }
                }
            }           

        }


        /* This method is called when the exit option from the menu file is pressed*/

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            {
                if (MessageBox.Show("Do you want to exit?", "Rebides Application",
                      MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                      == DialogResult.Yes)
                {
                    Application.Exit();
                }
            }
        }


        /* This method is called when the crete database option from the menu database is pressed*/

        private void createDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bd.createDB();
            MessageBox.Show("The Rebides database has been created", "Rebides Application",
            MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            createDatabaseToolStripMenuItem.Enabled = false;
        }


        /* This method is called to verify if the database is created*/

        public void verifyDB()
        {

            string arquivo = "Rebides.db";

            if (File.Exists(arquivo) == false)
            {
                MessageBox.Show("In order to continue, the application needs the database 'Rebides.db'. You have to create the database first", "Rebides Application",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

            }

            else {

                createDatabaseToolStripMenuItem.Enabled = false;
            }


        }

    }
}
